#include<stdio.h>
#include<stdlib.h>
#include "lame/lame.h"

typedef struct _MP3FrameHeader{
	unsigned char check1:8;//同步0xff

	unsigned char crc:1;//0-校验 1-不校验
	unsigned char lv:2;//00- 01-L3 10-L2 11-L1
	unsigned char mv:2;//00-M2.5 01- 10-M2 11-M1
	unsigned char check2:3;//同步0x7

	unsigned char check3:1;//保留1
	unsigned char padding:1;//帧长调节
	unsigned char persample:2;//采样率
	unsigned char bitrate:4;//比特率

	unsigned char emphasi:2;//强调模式
	unsigned char ori:1;//原版
	unsigned char copyright:1;//版权
	unsigned char ext:2;//扩充模式
	unsigned char modex:2;//声道模式
}MP3FrameHeader;
int mp3topcm(){
    hip_t hip= hip_decode_init();
    MP3FrameHeader head={0};
	int count=0;
	unsigned char*databuff=(unsigned char*)malloc(210);
	short pcmbuff[2][20480];
	int mp3bytes=0;
	int smples=0;
	FILE*pMP3=fopen("test.mp3","rb");
	FILE*pPCM=fopen("testend.pcm","wb");
	if (pMP3==NULL||pPCM==NULL){
		printf("open err\n");
		return 0;
	}
	printf("%s\n",get_lame_version());
	while((mp3bytes=fread(databuff,1,210,pMP3))>0){
		smples=hip_decode(hip,databuff,mp3bytes,pcmbuff[0],pcmbuff[1]);
		if(smples>0){
			fwrite(pcmbuff[0],sizeof(short),smples,pPCM);
		}
	}
	free(databuff);
	//free(pcmbuff);

	fclose(pMP3);
	fclose(pPCM);

	hip_decode_exit(hip);
    return 0;
}
